package ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;
import javax.swing.table.DefaultTableModel;
import javax.swing.AbstractListModel;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Color;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.border.LineBorder;

import mainpackage.Category;

import java.awt.Checkbox;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Insets;

public class MainWindow extends JFrame {

	private MainPresenter presenter;

	// private JFrame frame;
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel topPanel;
	private JPanel mainViewPanel;
	private JToggleButton monthToggleButton;
	private JToggleButton weekToggleButton;
	private JPanel topRightButtonsPanel;
	private JButton newCategoryButton;
	private JLabel lblCategories;
	private JButton leftButton;
	private JButton rightButton;
	private JPanel categoriesPanel;
	private JPanel monthPanel;
	private JPanel weekPanel;
	private JPanel categoriesLabelPanel;

	/**
	 * Launch the application.
	 * 
	 * @throws UnsupportedLookAndFeelException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		presenter = new MainPresenter(this);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		setBounds(100, 100, 510, 350);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		leftPanel = new JPanel();
		getContentPane().add(leftPanel, BorderLayout.WEST);
		leftPanel.setLayout(new BorderLayout());

		newCategoryButton = new JButton("New category");
		newCategoryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter.addCategoryClicked();
			}
		});
		leftPanel.add(newCategoryButton, BorderLayout.SOUTH);

		categoriesPanel = new JPanel();
		categoriesPanel.setBackground(new Color(250, 250, 250));
		leftPanel.add(categoriesPanel, BorderLayout.CENTER);
		categoriesPanel.setLayout(new GridLayout(10, 1, 0, 0));
		
		//*******************************
		JButton newTodoButton = new JButton("New todo(temp)");
		newTodoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter.addTodoClicked();
			}
		});
		categoriesPanel.add(newTodoButton);
		
		JButton editTodoButton = new JButton("Edit first todo(temp)");
		editTodoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				presenter.editTodoClicked();
			}
		});
		categoriesPanel.add(editTodoButton);

		//*****************************
		categoriesLabelPanel = new JPanel();
		categoriesLabelPanel.setPreferredSize(new Dimension(10, 35));
		leftPanel.add(categoriesLabelPanel, BorderLayout.NORTH);
		categoriesLabelPanel.setLayout(new BorderLayout());

		lblCategories = new JLabel("Categories:");
		lblCategories.setHorizontalAlignment(SwingConstants.CENTER);
		categoriesLabelPanel.add(lblCategories, BorderLayout.CENTER);
		lblCategories.setFont(new Font("Tahoma", Font.BOLD, 15));

		rightPanel = new JPanel();
		getContentPane().add(rightPanel, BorderLayout.CENTER);
		rightPanel.setLayout(new BorderLayout());

		topPanel = new JPanel();
		topPanel.setPreferredSize(new Dimension(10, 35));
		rightPanel.add(topPanel, BorderLayout.NORTH);
		topPanel.setLayout(new BorderLayout());

		topRightButtonsPanel = new JPanel();
		topPanel.add(topRightButtonsPanel, BorderLayout.EAST);

		leftButton = new JButton("<");
		topRightButtonsPanel.add(leftButton);

		rightButton = new JButton(">");
		topRightButtonsPanel.add(rightButton);

		monthToggleButton = new JToggleButton("Month");
		topRightButtonsPanel.add(monthToggleButton);
		monthToggleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				presenter.monthViewButtonClicked();
			}
		});

		weekToggleButton = new JToggleButton("Week");
		topRightButtonsPanel.add(weekToggleButton);
		weekToggleButton.setSelected(true);
		weekToggleButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				presenter.weekViewButtonClicked();
			}
		});

		mainViewPanel = new JPanel();
		rightPanel.add(mainViewPanel, BorderLayout.CENTER);
		mainViewPanel.setLayout(new CardLayout());

		/*monthPanel = new Cal();*/
		monthPanel = new JPanel();
		monthPanel.setBackground(Color.RED);
		mainViewPanel.add(monthPanel, "MONTH_PANEL");
		weekPanel = new WeekView();
		//weekPanel.setBackground(Color.BLUE);// temp
		mainViewPanel.add(weekPanel, "WEEK_PANEL");
		
		changeMainPanelContents("WEEK_PANEL");
	}

	/*
	 * public String getSelectedViewName() { if(monthToggleButton.isSelected()){
	 * return "month"; } else if (weekToggleButton.isSelected()){ return "week";
	 * } return null; }
	 */

	public JPanel getMainViewPanel() {
		return mainViewPanel;
	}

	public void setWeekButtonSelected(boolean arg) {
		weekToggleButton.setSelected(arg);
	}

	public void setMonthButtonSelected(boolean arg) {
		monthToggleButton.setSelected(arg);
	}

	public MainPresenter getPresenter() {
		return presenter;
	}

	public void changeMainPanelContents(String contentsPanelName) {
		CardLayout c = (CardLayout) (getMainViewPanel().getLayout());
		c.show(getMainViewPanel(), contentsPanelName);
	}

	public void addCheckBoxPanel(JPanel panel) {
		categoriesPanel.add(panel);
	}
	
	public void removeCheckBoxPanel(JPanel panel){
		categoriesPanel.remove(panel);
	}
	

}
